package com.dhsc.service.impl.xtqxgl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.xtqxgl.BqhcgzVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.xtqxgl.IBqhcgzDao;
import com.dhsc.service.xtqxgl.IBqhcgzService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BqhcgzServiceImpl
 * @Description: 标签互斥规则
 * @date: 2023/11/10 11:45
 * @author: gx
 */
@Service
@Slf4j
public class BqhcgzServiceImpl implements IBqhcgzService {

    @Autowired
    private IBqhcgzDao bqhcgzDao;

    @Override
    public LayuiTableVo queryBqhcgz(BqhcgzVo bqhcgzVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(bqhcgzVo.getPage(), bqhcgzVo.getLimit());
            List<BqhcgzVo> list = bqhcgzDao.queryBqhcgz(bqhcgzVo);
            PageInfo<BqhcgzVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询标签互斥规则信息出错",e);
            throw new MyException("查询标签互斥规则信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryGzname() {
        ResponseObject obj = new ResponseObject();
        try {
            List<BqhcgzVo> list = bqhcgzDao.queryGzname();
            obj.setData(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询规则名称信息出错", e);
            throw new MyException("查询规则名称信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject querySxname() {
        ResponseObject obj = new ResponseObject();
        try {
            List<BqhcgzVo> list = bqhcgzDao.querySxname();
            obj.setData(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询属性名称信息出错", e);
            throw new MyException("查询属性名称信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveBqhcgz(BqhcgzVo bqhcgzVo) {
        ResponseObject obj = new ResponseObject();
        try {
            List<BqhcgzVo> list = bqhcgzDao.queryBqhcgz(bqhcgzVo);
            if (CommonUtil.isNotEmptyList(list)){
                obj.setStatus(202);
                obj.setMessage("属性选择中存在已添加的数据");
                return obj;
            }
            String[] roleids = bqhcgzVo.getRoleid().split(",");
            List<BqhcgzVo> bqhcgzVos = new ArrayList<>();
            for (int i = 0; i < roleids.length; i++) {
                BqhcgzVo bqhcgz = new BqhcgzVo();
                bqhcgz.setBh(bqhcgzVo.getBh());
                bqhcgz.setName(bqhcgzVo.getName());
                bqhcgz.setRoleid(roleids[i]);
                bqhcgz.setUsername(bqhcgzVo.getUsername());
                bqhcgzVos.add(bqhcgz);
            }
            bqhcgzDao.insertBqhcgz(bqhcgzVos);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存标签互斥规则信息出错", e);
            throw new MyException("保存标签互斥规则信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteBqhcgz(BqhcgzVo bqhcgzVo) {
        ResponseObject obj = new ResponseObject();
        try {
            bqhcgzDao.deleteBqhcgz(bqhcgzVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除标签互斥规则信息出错", e);
            throw new MyException("删除标签互斥规则信息出错",e);
        }
        return obj;
    }
}
